﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XStudio.XCRF.SMSMesssage.Interfaces;
using System.Net;
using System.Collections.Specialized;
using XStudio.XCRF.Utilities.UTF8;

namespace XStudio.XCRF.SMSMesssage.Implements
{
    /// <summary>
    /// 飞信短信发送类
    /// </summary>
    public class FetionSMSSender : ISMSSender
    {
        /// <summary>
        /// 服务端URL地址
        /// </summary>
        public const string ServiceURL = "http://webosfx.sinaapp.com/index.php";
        /// <summary>
        /// 手机号码
        /// </summary>
        private string _Phone;
        /// <summary>
        /// 手机号码
        /// </summary>
        public string Phone
        {
            get { return _Phone; }
        }
        /// <summary>
        /// 密码
        /// </summary>
        private string _Password;
        /// <summary>
        /// 设置飞信账户信息
        /// </summary>
        /// <param name="phone">手机号码</param>
        /// <param name="password">飞信密码</param>
        public void InitAccount(string phone, string password)
        {
            _Phone = phone;
            _Password = password;
        }
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="targetPhone">目标电话号码</param>
        /// <param name="content">内容</param>
        public void Send(string targetPhone, string content)
        {
            WebClient client = new WebClient();
            client.Encoding = Encoding.UTF8;
            //.UploadValuesCompleted += UploadValuesCompleted;
            NameValueCollection postData = new NameValueCollection();
            postData.Add("phonenum", _Phone);
            postData.Add("pswd", _Password);
            postData.Add("to", targetPhone);
            postData.Add("content", content);
            postData.Add("send_submit", "Submit");    //这行坑爹，害我调试一下午

            //client.UploadValuesCompleted += new UploadValuesCompletedEventHandler(UploadValuesCompleted);
            client.UploadValues(ServiceURL, "POST", postData);
        }
        private void UploadValuesCompleted(object sender, UploadValuesCompletedEventArgs e)
        {
            Console.WriteLine(e.Result);
        }
        /// <summary>
        /// 构造方法，预设登录手机号和密码
        /// </summary>
        /// <param name="phone">手机号码</param>
        /// <param name="password">飞信密码</param>
        public FetionSMSSender(string phone, string password) : this()
        {
            InitAccount(phone, password);
        }
        /// <summary>
        /// 构造方法
        /// </summary>
        public FetionSMSSender()
        {

        }
    }
}
